package com.carkeeper.controller;

import com.carkeeper.dto.StoreDTO;
import com.carkeeper.pojo.StoreDetailPO;
import com.carkeeper.pojo.StorePO;
import com.carkeeper.service.StoreService;
import com.carkeeper.vo.Pager;
import com.carkeeper.vo.StoreQueryParam;
import com.carkeeper.vo.StoreVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/store")
public class StoreController {

    @Autowired
    public StoreService storeService;

    @PostMapping()
    public boolean create(@RequestBody StoreVO storeVO) {
        return storeService.create(storeVO);
    }

    @PutMapping("/{id}")
    public boolean update(@RequestBody StoreVO storeVO, @PathVariable("id")Long id) {
        storeVO.setId(id);
        return storeService.update(storeVO);
    }

    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable("id") Long id) {
        return storeService.delete(id);
    }

    @GetMapping()
    public Pager<StoreDTO> queryByParam(StoreQueryParam storeQueryParam) {
        Pager<StoreDTO> pager = storeService.queryPageByParam(storeQueryParam);
        return pager;
    }

    @GetMapping("/{id}")
    public StoreVO queryStoreById(@PathVariable("id") Long id) {
        StorePO storePO = storeService.queryById(id);
        StoreVO storeVO = new StoreVO();
        if (storePO == null) {
            return null;
        }
        BeanUtils.copyProperties(storePO, storeVO);
        StoreDetailPO storeDetailPO = storeService.queryDetailsById(id);
        if (storeDetailPO != null) {
            BeanUtils.copyProperties(storeDetailPO, storeVO);
        }
        return storeVO;
    }

}